<template>
    <div class="leftModelStylebox">

        <div class="leftItem">
            <p>选择风格</p>
            <div class="styleType">
                <span v-for="(styleType, index) in styleData.data" :class="index == seleStyleType ? 'sele' : ''"
                    @click="seleStyleType = index; changeStyle(0)">{{
                        styleType.name }}</span>
            </div>
            <div class="styleBox">
                <span class="styleItem" v-for="(styleItem, index) in  styleData.data[seleStyleType].children "
                    @click="changeStyle(index);">
                    <img :src="styleItem.icon" :class="seleStyle == index ? 'seleStyle' : ''" :alt="styleItem.name"
                        srcset="">
                    <p>{{ styleItem.name }}</p>
                </span>
            </div>
        </div>
        <div v-if="showFlag" class="leftItem">
            <p>参考图（选填）</p>
            <el-upload class="avatar-uploader" style="display: inline-block;"
                action="https://file.pencil-stub.com/user/file/upload" :data="uploadData" :show-file-list="false"
                :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon">
                    <Plus />
                </el-icon>
            </el-upload>
            <el-button v-if="imageUrl.length > 0" style="display: inline-block; cursor: pointer;"
                @click="imageUrl = ''; changeUpImg()">清除参考图</el-button>
        </div>
        <div class="leftItem">
            <p>尺寸</p>
            <span>
                <button :class="seleSize == index ? 'sizeBtn seleSizeBtn' : 'sizeBtn'" v-for="(sizeItem, index) in sizeData"
                    @click="changeSize(index)">
                    {{ sizeItem.name }}</button>
            </span>

        </div>
        <div class="leftItem">
            <p :class="!showFlag ? 'taskNumP' : ''">{{ !showFlag ? '图片数量:' : '图片数量' }}</p>
            <input v-if="!showFlag" v-model="seleNum" class="numberInput" type="number" name="quantity" :min="minNum"
                :max="maxNum" step="1" @change="changeNum(seleNum)">
            <span v-else>
                <button :class="seleNum == 1 ? 'sizeBtn seleSizeBtn' : 'sizeBtn'" @click="changeNum(1)">1</button>
                <button :class="seleNum == 2 ? 'sizeBtn seleSizeBtn' : 'sizeBtn'" @click="changeNum(2)">2</button>
                <button :class="seleNum == 3 ? 'sizeBtn seleSizeBtn' : 'sizeBtn'" @click="changeNum(3)">3</button>
                <button :class="seleNum == 4 ? 'sizeBtn seleSizeBtn' : 'sizeBtn'" @click="changeNum(4)">4</button>
                <span class="freeNum">
                    <p>自定义：</p>
                    <el-input type="number" @change="changeNum(seleNum)" v-model="seleNum"></el-input>
                </span>
            </span>

        </div>
    </div>
</template>
<script setup lang="ts">
import {
    ref,
    reactive,
    getCurrentInstance,
    onMounted,
    toRefs,
    toRaw,
} from "vue";
import { useRouter } from "vue-router";
import { getPublic } from "../../request/getPublic"; //获取静态资源
import $apiFun from "../../request/api";
import { ElMessage } from 'element-plus'
import type { UploadProps } from 'element-plus'
import { uploadFile } from '@/utils/file/handelFileUrl'
import { getUid } from "../../hooks/auth";
const showFlag = ref(false)//当前路由地址
const styleData = reactive({//风格数据
    data: [{ children: [] }] as any
})
const props = defineProps<{
    jsonData: any
}>()
//尺寸数据
const sizeData = ref([
    {
        id: 0,
        name: '1:1',
        value: [512, 512, 0]
    },
    {
        id: 1,
        name: '9:16',
        value: [405, 720, 1]
    },
    {
        id: 2,
        name: '16:9',
        value: [720, 405, 2]
    },
    {
        id: 3,
        name: '3:4',
        value: [405, 540, 3]
    },
    {
        id: 4,
        name: '4:3',
        value: [540, 405, 4]
    },
    {
        id: 5,
        name: '2:3',
        value: [480, 720, 5]
    }, {
        id: 6,
        name: '3:2',
        value: [720, 480, 6]
    },

])
const maxNum = ref(200)//最大图片数量
const minNum = ref(10)//最大图片数量
const seleStyleType = ref(0)//选择的风格类型
const seleStyle = ref(0)//选择的风格
const seleSize = ref(0)//选择的尺寸
const seleNum = ref(1)//选择的数量
const imageUrl = ref("")//icon地址--本地临时
const imageRes = ref("")//icon地址--线上地址
const uploadData = ref({
    content_type: 1,
    format: "",
    biz: "ArtsImg" + getUid()
}) as any//上传文件时带的参数
//定义要发送的emit事件
const emit = defineEmits(['changeStyle', 'changeSize', 'changeNum', 'openLoginFun', 'changeUpImg'])

//icon上传成功
const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    imageUrl.value = URL.createObjectURL(uploadFile.raw!)
    changeUpImg()
}
//上传icon前检验格式，大小和尺寸
const beforeAvatarUpload: UploadProps['beforeUpload'] = async (rawFile) => {
    uploadData.value.format = rawFile.name.substring(rawFile.name.lastIndexOf('.') + 1)
    console.log("rawFile", rawFile)
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('仅支持jpg格式!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 5) {
        ElMessage.error('最大支持 5MB 图片!')
        return false
    }
    return true
}
//上传了图生图的参考图
const changeUpImg = () => {
    console.log(imageUrl.value)
    emit('changeUpImg', imageUrl.value)
}

//改变风格
const changeStyle = (data: any) => {
    // 发送事件，theType就是发送出去的值
    seleStyle.value = data
    emit('changeStyle', styleData.data[seleStyleType.value].children[data])
}
//改变尺寸
const changeSize = (data: any) => {
    // 发送事件，theType就是发送出去的值
    seleSize.value = data
    emit('changeSize', sizeData.value[data].value)
}
//改变数量
const changeNum = (data: any) => {
    // 发送事件，theType就是发送出去的值
    data = data > maxNum.value ? maxNum.value : data < minNum.value ? minNum.value : data
    seleNum.value = data
    emit('changeNum', data)
}
//获取风格
const getStyle = async () => {
    await $apiFun.Model2({ channel: 6, mateType: 1 }).then(res => {
        console.log(res)
        if (res.code == 1001) {
            emit('openLoginFun', true)
            return
        }
        if (res.code == 200) {
            console.log(res)
            styleData.data = res.data
            let num = 1
            let size = 0
            let style = 0
            if (props.jsonData) {
                if (props.jsonData.imgNum) {
                    num = props.jsonData.imgNum
                }
                if (props.jsonData.size) {
                    size = props.jsonData.size
                }
                if (props.jsonData.style_id) {
                    styleData.data.forEach((styleType, index1) => {
                        styleType.children.forEach((style, index2) => {
                            if (style.id == props.jsonData.style_id) {
                                seleStyleType.value = index1
                                style = index2
                            }
                        });
                    });
                }
            }
            changeStyle(style)
            changeSize(size)
            changeNum(num)
        }
    })
}
onMounted(() => {
    if (useRouter().currentRoute.value.name?.indexOf('addTask') != -1) {
        showFlag.value = false
    } else {
        showFlag.value = true
    }
    maxNum.value = 200
    minNum.value = 1
    getStyle()

});
</script>
<!-- 上传图片 -->
<style scoped>
:deep(.avatar-uploader .avatar) {
    width: 100px;
    height: 100px;
    display: block;
}

:deep(.avatar-uploader .el-upload) {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

:deep(.avatar-uploader .el-upload:hover) {
    border-color: var(--el-color-primary);
}

:deep(.el-icon.avatar-uploader-icon) {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    text-align: center;
}
</style>

<!-- 中间和左边 -->
<style scoped>
:deep(.freeNum .el-input) {
    display: inline-block;
    width: 80px;
    padding-right: 0;
}

:deep(.freeNum .el-input__wrapper) {
    padding-right: 0;
}


.leftItem .freeNum p {
    font-size: 12px;
    display: inline-block;
    font-weight: unset;
    line-height: unset;
    margin: unset;
}

.freeNum {
    display: inline-block;
}

.taskNumP {
    display: inline-block;
    vertical-align: middle;
}

input:focus {
    outline: none;
}

.numberInput {
    display: inline-block;
    margin-left: 10px;
    width: 20%;
    height: 30px;
    opacity: 1;
    border-radius: 5px;
    background: rgba(255, 255, 255, 1);
    border: 1px solid rgba(229, 229, 229, 1);
    font-size: 12px;
    font-weight: 400;
    color: rgba(166, 166, 166, 1);
    text-align: center;
}

.leftItem .seleSizeBtn {
    border: 1px solid rgba(255, 195, 0, 1);
}

.leftItem .sizeBtn:nth-child(1) {
    margin-left: 0
}

.sizeBtn {
    width: 48px;
    height: 29px;
    margin: 0 12px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 1);
    border: 1px solid rgba(229, 229, 229, 1);
    font-size: 12px;
    color: rgba(128, 128, 128, 1);
}

.styleBox .styleItem p {
    margin: 0;
    font-size: 13px;
    font-weight: 500;
    color: rgba(56, 56, 56, 1);
    text-align: center;
}

.styleItem .seleStyle {
    border-color: rgba(255, 195, 0, 1);
}

.styleItem img {
    width: calc(80%);
    aspect-ratio: 1/1;
    border: 2px solid transparent;
    border-radius: 10px;
}

.styleItem {
    display: inline-block;
    width: 24%;
    box-sizing: border-box;
    text-align: center;
}

.styleBox {
    width: 100%;
    height: 250px;
    overflow-y: auto;
}

.styleType .sele {
    color: rgba(255, 195, 0, 1);
}

.styleType span {
    cursor: pointer;
    margin: 0 12px;
    /** 文本1 */
    font-size: 14px;
    font-weight: 500;
    color: rgba(128, 128, 128, 1);
}

.styleType {
    position: absolute;
    top: 0;
    right: 0;
    margin-top: 24px;
}


.leftItem p {
    font-size: 16px;
    font-weight: 500;
    line-height: 23.17px;
    color: rgba(0, 0, 0, 1);
    margin: 10px 0;
}

.leftItem {
    position: relative;
    padding: 10px 0;
}

.leftModelStylebox {
    position: relative;
    width: 100%;
    left: 0;
    overflow-y: auto;
}

.leftModelStylebox::-webkit-scrollbar {
    display: none;
    /* 隐藏滚动条 */
}
</style>

